
cc.Class({
    extends: require('Monster'),

    properties: {
        supLauncherL:require('SupLauncher'),
        supLauncherR:require('SupLauncher')
    }, 
    onLoad(){
        this._super();
        // if(global.gates!='1-1'){
            this.level=3;
        //     global.Event.emit('upBullet',this.level);
        // }
        // this.level =global.Data.skillLevel;
        // this.n_crazyTime = 4+4*global.Data.superLevel*global.Data.upgradeNew.crazy.value;
        this.n_crazyTime = 5
        this.n_convertDirection = false;
        this.n_convertTime = 0;
        this.b_isDie = false;
        this.empTime = 0;
        this.grow = 1;
        this.lossB=0;

        this.supNumber=global.supId;  //0,1两种辅助发射器

        this.supLauncherL.setOptions({sup:this.supNumber})
        this.supLauncherR.setOptions({sup:this.supNumber})

        this.weiliTime=0;
        this.addblood=20;
        this.fullBlood= global.Data.health;
        this.baozou= this.node.getChildByName('baozou');
        this.baozou.active=false;
        this.change= this.node.getChildByName('change1').getComponent(cc.Animation);

        if(this.level==4 /*&& (this.node.name=='player3' || this.node.name=='player4')*/){
            // this.changeBgSpeed(true)
            this.change.play(this.change._clips[0].name);
            // if(this.node.name=='player4'){
                this.baozou.active=true;
            // }
        }
        this.launcher.setSkillParentNode(global.GameLoop.GameMgr.no_BulletSelfLayer)
        for(let i=0;i<global.Data.prop.length;i++){
            if(global.Data.prop[i]){
                this.onGetProp({
                    type:i==0?i:i==1?3:1
                })
            }
        }
    },

    setOptions(option,gameUI){
        this._super({cfg:option});
        this.bloodLevel=1;
        this.blood=this.fullBlood;
        this.s_gameUI = gameUI;
        this.isIgnore=false;
    },

    startFire(){ //装载子弹
        
        // SoundMgr.playSound('Audio/change_2')
        
        this.launcher.reset();

        var skills = this.logicOptions.status[Math.min(4,this.level)];
        for(let i=0;i<skills.length;i++){
            if(skills[i].name){
                skills[i].speed=global.SkillConfig[skills[i].name].speed*(global.Data.speedLevel*0.01+1)
                skills[i].emissionRate=Math.floor(global.SkillConfig[skills[i].name].emissionRate*(global.Data.speedLevel*0.01+1))
            }
            
        }
        this.launcher.addLauncher(skills);
        cc.log('子弹装载完成')
        if(this.supNumber>=0){
            this.supLauncherL.startFire();
            this.supLauncherR.startFire();
        }
        
    },

    stopFire(){
        cc.log('stopFire')
        this.isIgnore=true;
        this.change.play(this.change._clips[1].name);
        this.baozou.active=false;
        this.launcher.reset();
        this.supLauncherL.stopFire();
        this.supLauncherR.stopFire();
    },

    die(){
        this.b_isDie = true;
    },

    setEmp(time){
        // this.unscheduleAllCallbacks()
        this.isIgnore1=true;
        //  this.isIgnore=false;
        this.node.getChildByName('sheild').active = true;
        this.empTime=time;
    },

    onAttack(bullet){ //受到攻击
        var multiple=1
        if(!global.canHit || this.isIgnore){
            return
        }
        // return
        if(global.gates==0){
            multiple=global.GameLoop.GameMgr.getAllTime()/100+1
        }else{
            multiple=(parseInt(global.gates.split('-')[0])*4-4+parseInt(global.gates.split('-')[1])-1)*0.25+1
        }

        this.lossB=bullet.attack*multiple
        if(this.lossB>this.fullBlood*2/3){
            this.lossB=this.fullBlood*2/3;
        }
        if(!bullet.attack){
            cc.warn('子弹没有伤害?????????????')
        }
        CC_WECHATGAME && wx.vibrateLong()
        if(this.isIgnore1){ //失去护盾
            this.isIgnore1=false
            this.node.getChildByName('sheild').active = false;
            this.empTime=1
            this.setIgnore(true,2)
            SoundMgr.playSound('Audio/Item_shield');
            global.GameLoop.GameMgr.onReleaseEmp();
            return
        }else if(this.blood>this.lossB){
            //血量
            this.setEmp(3);

            this.blood-=this.lossB;


            SoundMgr.playSound('Audio/Item_shield');
            // global.GameLoop.GameMgr.onReleaseEmp();
            this.setIgnore(true,3);
            global.Event.emit('playerBlood',{data:this.lossB/this.fullBlood*100,add:false});
            return;
        }
        global.Event.emit('playerBlood',{data:this.lossB/this.fullBlood*100,add:false});
        global.GameLoop.GameMgr.attackPlayer();
    },

    restart(){
        this.node.y = -cc.winSize.height/2 - 300;
        var self = this;
        
        this.node.runAction(cc.sequence(cc.delayTime(0.1),cc.callFunc(function(){
            self.node.getChildByName('launcher').active =true;
            self.node.getChildByName('supLauncher').active =true;
            self.startFire();
        })));
        this.node.runAction(cc.moveBy(0.5,cc.v2(0,600)))
    },

    changeBgSpeed(data){
        global.GameLoop.GameMgr.bgMgr.changeSpeed(data);
    },

    onGetProp(no){
        if(this.b_isDie){
            return false;
        }
        SoundMgr.playSound('Audio/coin_get')
        // cc.log('拾取到道具:',no.type,this.level);
        
        // global.Event.emit('GETPROP',{type:no.type})
        this.scheduleOnce(()=>{
            if(no.type == 0){
                //暴走
                // this.changeBgSpeed(true)
                this.level = Math.min(4,this.level+1);
                global.Event.emit('upBullet',this.level);
                if(this.level == 4){
                    this.n_crazyTime = 8
                    if(this.baozou.active==false){
                        global.Event.emit('crazyTime',1);
                        SoundMgr.playSound('Audio/Transform_hero_0');
                        this.change.play(this.change._clips[0].name);
                    }
                    this.scheduleOnce(()=>{
                        this.baozou.active=true;
                        this.startFire();
                    },0.5)
                }else{
                    SoundMgr.playSound('Audio/Item_up')
                    this.startFire();
                }
            } else if(no.type == 1 && global.gates!=-1){
                // 护盾
                global.GameLoop.GameMgr.keepEmp(10)
            } else if(no.type == 2 &&  global.gates!=-1){
                //僚机
                global.GameLoop.GameMgr.createOtherPlayer(global.planeId)
            }else if(no.type == 3 &&  global.gates!=-1){
                //武器升级
                this.weiliTime=10;
            }else if(no.type == 4 &&  global.gates!=-1){
                //加血
                if(this.fullBlood-this.lossB<this.blood){
    
                    global.Event.emit('playerBlood',{data:(this.fullBlood-this.blood)*100/this.fullBlood,add:true});
                    this.blood=this.fullBlood;
                }else{
                    this.blood+=this.lossB;
                    global.Event.emit('playerBlood',{data:this.lossB*100/this.fullBlood,add:true});
                }
                
            }
        },0.5)
        
        global.GameLoop.GameMgr.s_gameUI.propMove(this.node.getPosition(),no.type)
        return true;
    },

    moveBy(p){
        if(cc.isPauseGame){
            return;
        }
        var dx = this.node.x+p.x;
        var dy = this.node.y+p.y;
        if(this.n_convertDirection){
            
        }
        if(dx >= -cc.winSize.width/2 && dx <= cc.winSize.width/2 && dy >= -cc.winSize.height/2 && dy<=cc.winSize.height/2){
            this.node.x += p.x;
            this.node.y += p.y
        }
        global.Event.emit('PLANEPOSITION',{x:this.node.x,y:this.node.y})
    },
    gameUpdate(dt){
        this._super(dt);
        this.empTime-=dt;
        this.weiliTime-=dt;
        global.GameLoop.GameMgr.s_gameUI.showHudun(-this.empTime/10)
        global.GameLoop.GameMgr.s_gameUI.showWeili(-this.weiliTime/10)
        if(this.empTime<=0){
            this.isIgnore1=false;
            this.node.getChildByName('sheild').active = false
        }
        if(this.level >= 4){
            if(this.n_crazyTime <0){
                //back变形动画，暴走光衣隐藏
                // this.changeBgSpeed(false)
                // if(this.node.name=='player4' || this.node.name=='player3'){
                    SoundMgr.playSound('Audio/Transform_hero_1');
                    this.change.play(this.change._clips[1].name);
                    // if(this.node.name=='player4'){
                        this.baozou.active=false;
                    // }
                // }
                
                global.Event.emit('crazyTime',0);
                this.level = 3;
                this.startFire();
            } else {
                this.n_crazyTime -= dt;
                global.Event.emit('crazyTime',this.n_crazyTime/(8));
                global.GameLoop.GameMgr.s_gameUI.showBaozou(-this.n_crazyTime/(8))
            }
        }
    }
});
